import json


class MessageData:
    def __init__(self, data: str):
        self.data = data    # 原始数据
        self.message_type = ''  # 消息类型
        self.group_id = 0   # 群号
        self.uid = 0    # 发信息人的uid
        self.self_id = 0    # 自己id
        self.message = []   # 消息
        self.get_message_type()
        self.get_id()
        self.get_message()
        self.get_selfid()


    def get_message_type(self):
        message_type_index = self.data.find("message_type")
        if message_type_index != -1:
            message_sub = self.data[message_type_index:]
            message_sub_index_mh = message_sub.find(":")
            message_sub_index_dh = message_sub.find(",")
            if message_sub_index_mh != -1 and message_sub_index_dh != -1:
                self.message_type = (message_sub[message_sub_index_mh:message_sub_index_dh].
                                     replace(':', '').
                                     replace(' ', '').
                                     replace('\"', ''))

    def get_id(self):
        uid_index = self.data.find("user_id")
        if uid_index != -1:
            uid_sub = self.data[uid_index:]
            uid_sub_index_mh = uid_sub.find(":")
            uid_sub_index_dh = uid_sub.find(",")
            if uid_sub_index_mh != -1 and uid_sub_index_dh != -1:
                self.uid = int(uid_sub[uid_sub_index_mh:uid_sub_index_dh].
                               replace(':', '').
                               replace(' ', '').
                               replace('\"', ''))
        if self.message_type == 'group':
            gid_index = self.data.find("group_id")
            if gid_index != -1:
                gid_sub = self.data[gid_index:]
                gid_sub_index_mh = gid_sub.find(":")
                gid_sub_index_dh = gid_sub.find(",")
                if gid_sub_index_mh != -1 and gid_sub_index_dh != -1:
                    self.group_id = int(gid_sub[gid_sub_index_mh:gid_sub_index_dh].
                                        replace(':', '').
                                        replace(' ', '').
                                        replace('\"', ''))

    def get_message(self):
        message_index = self.data.find("message")
        temp_list = []
        message_sub = self.data[message_index:]
        message_sub_index_l = message_sub.find('[')
        temp_l = message_sub.find('[')
        temp_r = message_sub.find(']')
        # [[[]]]  [[][][]] [[]
        while temp_l != -1 and temp_r != -1 and message_sub.find('[', temp_l+1, temp_r) != -1:
            temp_r = message_sub.find(']', temp_r+1, len(message_sub))
            temp_l = message_sub.find('[', temp_l+1, temp_r)
        if temp_l != -1 and temp_r != -1:
            self.message = json.loads(message_sub[message_sub_index_l:temp_r+1])

    def get_selfid(self):
        self_id_index = self.data.find("self_id")
        if self_id_index != -1:
            self_id_sub = self.data[self_id_index:]
            self_id_sub_index_mh = self_id_sub.find(":")
            self_id_sub_index_dh = self_id_sub.find(",")
            if self_id_sub_index_mh != -1 and self_id_sub_index_dh != -1:
                self.self_id = int(self_id_sub[self_id_sub_index_mh:self_id_sub_index_dh].
                               replace(':', '').
                               replace(' ', '').
                               replace('\"', ''))

    def print_data(self):
        print("Message Type: " + str(self.message_type) + " "
              "Group ID: " + str(self.group_id) + " "
              "UID: " + str(self.uid) + " "
              "Self ID: " + str(self.self_id) + " "
              "Message: " + str(self.message))

